//
//  EHImageViewRow.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/8/23.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import Eureka

class EHImageViewConfiguration {
    var image: UIImage?

    func setImage(_ image: UIImage) -> Self { self.image = image; return self }
}

class EHImageViewCell: Cell<Bool>, CellType {
    var bgImageView: UIImageView!
    var bgMaskView: UIView!

    override func setup() {
        setupBGImageView()
        setupMaskView()
        configure()
        height = { 220 }
    }

    private func configure() {
        guard let config = (row as? EHImageViewRow)?.configuration else { return }

        bgImageView.image = config.image
    }

    private func setupMaskView() {
        let maskV = UIView()
        maskV.alpha = 0.68
        maskV.backgroundColor = .black
        maskV.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(maskV)
        NSLayoutConstraint.activate([
            maskV.leftAnchor.constraint(equalTo: contentView.leftAnchor),
            maskV.rightAnchor.constraint(equalTo: contentView.rightAnchor),
            maskV.topAnchor.constraint(equalTo: contentView.topAnchor),
            maskV.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
            ])
        bgMaskView = maskV
    }

    private func setupBGImageView() {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
        imageV.layer.masksToBounds = true
        imageV.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(imageV)
        NSLayoutConstraint.activate([
            imageV.leftAnchor.constraint(equalTo: contentView.leftAnchor),
            imageV.rightAnchor.constraint(equalTo: contentView.rightAnchor),
            imageV.topAnchor.constraint(equalTo: contentView.topAnchor),
            imageV.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
            ])
        bgImageView = imageV
    }
}

final class EHImageViewRow: Row<EHImageViewCell>, RowType {
    var configuration: EHImageViewConfiguration?
}
